package light.mvc.service.doctor.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import light.mvc.dao.BaseDaoI;
import light.mvc.model.item.TchemicalExam;
import light.mvc.model.register.TchargeChemicalExam;
import light.mvc.model.register.Tregister;
import light.mvc.model.register.TregisterCharge;
import light.mvc.page.doctor.ChargeChemicalExam;
import light.mvc.page.doctor.RegisterCharge;
import light.mvc.service.doctor.ChargeChemicalExamServiceI;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ChargeChemicalExamServiceImpl implements ChargeChemicalExamServiceI {

	@Autowired
	private BaseDaoI<Tregister> registerDao;
	
	@Autowired
	private BaseDaoI<TregisterCharge> registerChargeDao;
	
	@Autowired 
	private BaseDaoI<TchemicalExam> chemicalExamDao;
	
	@Autowired 
	private BaseDaoI<TchargeChemicalExam> chargeChemicalExamDao;
	
	@Override
	public List<RegisterCharge> getRegisterCharge(Integer registerId) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("registerId", registerId);
		
		String hql = "select t from TregisterCharge t join t.register register " +
					"join fetch t.chemicalExam where register.id = :registerId";
		List<TregisterCharge> list = registerChargeDao.find(hql,params);
		List<RegisterCharge> registerCharges = new ArrayList<RegisterCharge>();
		
		for(TregisterCharge t : list) {
			RegisterCharge registerCharge = new RegisterCharge();
			
			registerCharge.setChemicalExamItem(t.getChemicalExam().getChemicalExamItem());

			registerCharge.setId(t.getId());
			registerCharge.setAddTime(t.getAddTime());
			registerCharge.setChargeTime(t.getChargeTime());
			
			if(t.getChargeChemicalExam() != null) {
				registerCharge.setChargeChemicalExamId(t.getChargeChemicalExam().getId());
				registerCharge.setChemicalExamResult(t.getChargeChemicalExam().getResult());
				registerCharge.setChemicalExamAddTime(t.getChargeChemicalExam().getAddTime());
			}
			

			registerCharges.add(registerCharge);
		}
		return registerCharges;
	}
	
	@Override
	public List<RegisterCharge> getRegisterChargeByPatientId(Integer patientId,Date timeSegment) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("patientId", patientId);
		
		String hql = "select t from TregisterCharge t join t.register register " +
					"join register.patient patient join t.chemicalExam " +
					"where patient.id = :patientId ";
		if(timeSegment != null) {
			params.put("timeSegment", timeSegment);
			hql += "and register.timeSegment = :timeSegment";
		}
		List<TregisterCharge> list = registerChargeDao.find(hql,params);
		List<RegisterCharge> registerCharges = new ArrayList<RegisterCharge>();
		
		for(TregisterCharge t : list) {
			RegisterCharge registerCharge = new RegisterCharge();
			
			registerCharge.setChemicalExamItem(t.getChemicalExam().getChemicalExamItem());

			registerCharge.setId(t.getId());
			registerCharge.setAddTime(t.getAddTime());
			registerCharge.setChargeTime(t.getChargeTime());
			
			if(t.getChargeChemicalExam() != null) {
				registerCharge.setChargeChemicalExamId(t.getChargeChemicalExam().getId());
				registerCharge.setChemicalExamResult(t.getChargeChemicalExam().getResult());
				registerCharge.setChemicalExamAddTime(t.getChargeChemicalExam().getAddTime());
			}
			
			registerCharge.setTimeSegment(t.getRegister().getTimeSegment());
			registerCharge.setSeq(t.getRegister().getSeq());
			registerCharge.setDoctorName(t.getRegister().getDoctor().getName());
			registerCharge.setDepartmentName(t.getRegister().getDoctor().getDepartment().getName());
			
			registerCharges.add(registerCharge);
		}
		return registerCharges;
	}
	
	@Override
	public void addCharge(Integer registerId,Integer chemicalExamId) {
		TchemicalExam chemicalExam = chemicalExamDao.get(TchemicalExam.class, chemicalExamId);
		Tregister register = registerDao.get(Tregister.class, registerId);
		
		TregisterCharge t = new TregisterCharge();
		
		t.setChemicalExam(chemicalExam);
		t.setRegister(register);
		t.setAddTime(new Date());
		
		registerChargeDao.save(t);
	}
	
	@Override
	public void charge(Integer registerChargeId) {
		TregisterCharge t = registerChargeDao.get(TregisterCharge.class, registerChargeId);
		
		t.setChargeTime(new Date());
		
		registerChargeDao.update(t);
	}
	
	@Override
	public void addChemicalExam(ChargeChemicalExam chargeChemicalExam) throws Exception {
		TregisterCharge registerCharge = registerChargeDao.get(TregisterCharge.class, chargeChemicalExam.getRegisterChargeId());
		if(registerCharge.getChargeTime() == null) {
			throw new Exception("尚未缴费");
		}
		TchargeChemicalExam t = registerCharge.getChargeChemicalExam();
		if(t != null) {
			t.setResult(chargeChemicalExam.getResult());
			chargeChemicalExamDao.update(t);
		} else {
			TchargeChemicalExam t2 = new TchargeChemicalExam();
			t2.setAddTime(new Date());
			t2.setResult(chargeChemicalExam.getResult());
			
			registerCharge.setChargeChemicalExam(chargeChemicalExamDao.get(TchargeChemicalExam.class, chargeChemicalExamDao.save(t2)));
			registerChargeDao.save(registerCharge);
		}
		
		
	}
}
